//
//  ViewController.m
//  20530网络获取图片
//
//  Created by student on 16/3/23.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;
@property(nonatomic,strong)NSArray *arr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
    // 练习要求:
    // 使用gcd及performSelectorInBackground方法实现多线程下载图片并显示到界面上,不能卡主界面,并且gcd采用不同的方式(串行、并发)都实现一下

}


- (IBAction)btn1:(id)sender {
    for (int i =0; i<self.arr.count; i++) {
        //网址字符串
        NSString *urlStr=self.arr[i];
        //网址对象
        NSURL *url=[NSURL URLWithString:urlStr];
        //获取数据
        NSData *data=[NSData dataWithContentsOfURL:url];
        //图像对象
        UIImage *image=[UIImage imageWithData:data];
        UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
        imageView.frame=CGRectMake(100, 100, 100, 100);
        [self.view addSubview:imageView];
        
    }
    //一起出来
}



//GCD方式
- (IBAction)btn2:(id)sender {
    //定义一个自定义串行队列
    dispatch_queue_t queue=dispatch_queue_create("myQueue", NULL);
    
    //自定义并发任务
//    dispactch_queue_t queue=dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
    
    //定义全局并发队列
//    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);//普通优先级
    
    
    
    // 1.往queue中放入一个任务，循环了10次 (串行的)
    dispatch_async(queue, ^{
        for (int i =0; i<self.arr.count; i++) {
            //网址字符串
            NSString *urlStr=self.arr[i];
            //网址对象
            NSURL *url=[NSURL URLWithString:urlStr];
            //获取数据
            NSData *data=[NSData dataWithContentsOfURL:url];
            //图像对象
            UIImage *image=[UIImage imageWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
                imageView.frame=CGRectMake(100, 100, 100, 100);
                [self.view addSubview:imageView];
            });
           
        }
    });
    
    
    //2.往queue中放入10个任务,串行队列是顺序执行的,并发队列并发执行
    for (int i =0; i<self.arr.count; i++) {
        //网址字符串
        NSString *urlStr=self.arr[i];
        //网址对象
        NSURL *url=[NSURL URLWithString:urlStr];
        
        dispatch_async(queue, ^{
            //获取数据
            NSData *data=[NSData dataWithContentsOfURL:url];
            //图像对象
            UIImage *image=[UIImage imageWithData:data];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
                imageView.frame=CGRectMake(100, 100, 100, 100);
                [self.view addSubview:imageView];
            });
        });
    
    }
    
    // 定义了10个自定义串行queue，这些队列是并发的，对里面的任务是串行（理论上不应该这么去做）
    
    
//    for (int i =0; i<self.arr.count; i++) {
//        
//        dispactch_queue_t queue =dispatch_queue_create("myQueue", NULL);
//        //网址字符串
//        NSString *urlStr=self.arr[i];
//        //网址对象
//        NSURL *url=[NSURL URLWithString:urlStr];
//        
//        dispatch_async(queue, ^{
//            //获取数据
//            NSData *data=[NSData dataWithContentsOfURL:url];
//            //图像对象
//            UIImage *image=[UIImage imageWithData:data];
//            
//            dispatch_async(dispatch_get_main_queue(), ^{
//                UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
//                imageView.frame=CGRectMake(100, 100, 100, 100);
//                [self.view addSubview:imageView];
//            });
//        });
//        
//    }
    
    
}
- (IBAction)btn3:(id)sender {
    [self performSelectorInBackground:@selector(doInBack) withObject:nil];
}
- (void)doInBack
{
    for (int i = 0;i<self.arr.count;i++)
    {
        // 网址字符串
        NSString *urlStr = self.arr[i];
        // 网址对象
        NSURL *url = [NSURL URLWithString:urlStr];
        // 获取数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 图像对象
        UIImage *image = [UIImage imageWithData:data];
        
        [self performSelectorOnMainThread:@selector(addImageViewWithArr:) withObject:@[image,@(i)] waitUntilDone:NO];
    }
}

- (void)addImageViewWithArr:(NSArray *)arr
{
    UIImage *image = arr[0];
    int i = [arr[1] intValue];
    // 图像视图
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    imgView.frame = CGRectMake((i%3)*100 ,(i/3)*100,100, 100);
    // 添加到视图
    [self.view addSubview:imgView];
}

    


@end
